<template>
    <div id="retreatCard">
        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <span v-if="editType==1">待处理在途库存: 剩余{{scopeData.totalLess}}件</span>
                <span v-else-if="editType ==2">出库仓: {{scopeData.senderStore}}</span>
                <span v-else>入库仓: {{scopeData.receiverStore}}</span>
                <el-button v-if="((editType==1 && !edit1) || (editType==2 && !edit2) ||(editType==3 && !edit3)) && activeName == 'first' && type == 'edit'" style="float: right; padding: 3px 0" type="text" size="mini" @click="handleClick">操 作</el-button>
            </div>
            <div>
                <el-tree
                :data="data"
                :show-checkbox="listType == 'list' ? ( (editType == 1 && edit1) ? true : (editType == 2 && edit2 ? true : (editType == 3 && edit3) ? true : false) ): false"
                node-key="skuCode"
                default-expand-all
                ref="retreatTpl"
                :props="defaultProps"
                @check-change="handleCheckChange"
                :expand-on-click-node="false"
                :filter-node-method="filterTreeNode"
                style="margin-bottom: 10px; border-bottom: 1px solid #eee">
                    <span class="custom-tree-node" slot-scope="{ node, data }">
                        <span v-if="data">{{ data.level == 1 ? (data.brand ) : data.level == 2 ? (data.code ) : data.level ==3 ? (data.skuCode + data.specName) : '' }}</span>
                        <span v-if="data.goodsQty && data.level == 3 " >
                            <el-input-number v-if=" data.goodsQtyPc >1 && (((editType==1 && edit1) || (editType==2 && edit2) ||(editType==3 && edit3)) && activeName == 'first' &&type == 'edit' ) " size="mini" v-model="data.goodsQty" controls-position="right" :min="1" :max="data.goodsQtyPc"></el-input-number>
                            <span style="color: red" v-else>{{data.goodsQty}} 件</span>
                        </span>
                    </span>
                </el-tree>
                <div style="margin-bottom: 0px" v-if="((editType == 1 && edit1) || (editType==2 && edit2) || (editType==3 && edit3) )&& type=='edit'">
                    <el-button type="primary" size="mini" @click="handleClickFirst">{{editType == 1 ? '加至出库仓' : (editType == 2 || editType == 3 )? '加至在途' : ''}}</el-button>
                    <el-button type="primary" size="mini" @click="handleClickSecond">{{(editType == 1 || editType == 2 ) ? '加至入库仓'  : editType == 3 ? '加至出库仓' : ''}}</el-button>
                </div>
            </div>
        </el-card>
    </div>
</template>
<script>
    export default {
        props: ['treeData', 'editType', 'activeName', 'listType', 'scopeData', 'type'],
        computed: {
            data: {
                get() {
                    return this.treeData;
                },
                set() {}
            },
            edit1: {
                get() {
                    return this.$store.state.retreatTpl.editOn
                },set() {}
            },
            edit2: {
                get() {
                    return this.$store.state.retreatTpl.editOut
                },set() {}
            },
            edit3: {
                get() {
                    return this.$store.state.retreatTpl.editIn
                },set() {}
            }
        },
        data() {
            return {
                defaultProps: {
                    children: 'children',
                    disabled: (data,node) => {
                        if(data.level !=3) {
                            return false
                        }else{
                            if(data.goodsQty > 0) {
                                return false;
                            }else{
                                return true
                            }
                        }
                    }
                },
                selectData: [], 
            }
        },
        created() {
            this.$nextTick(() => {
                this.$refs.retreatTpl.filter()
            })
        },
        mounted() {
            this.$nextTick(() => {
                this.$refs.retreatTpl.filter()
            })
        },

        methods: {
            filterTreeNode(value, data, node) {
               
                if(data.level == 1) {
                      console.log(data, 'data1')
                    let level1Arr= []
                    if(data.children.length > 0) {
                        for(let i=0; i < data.children.length; i++) {
                            if(data.children[i].children.length > 0) {
                                for(let m=0; m< data.children[i].children.length; m++) {
                                    if(data.children[i].children[m].goodsQty > 0) {
                                        level1Arr.push(data.children[i].children[m].goodsQty )
                                    }else{ return false}
                                }
                            }else{
                                return false
                            }
                            if(level1Arr.length > 0) {
                                return true
                            }else{
                                return false
                            }
                        }
                    }else{
                        return false
                    }
                }else if(data.level == 2) {
                      console.log(data, 'data2')
                    let level2arr= []
                    if(data.children.length > 0) {
                        for(let k=0; k<data.children.length; k++) {
                            if(data.children[k].goodsQty > 0) {
                                level2arr.push(data.children[k].goodsQty)
                            }else{
                                return false;
                            }
                        }
                        if(level2arr.length > 0) {
                            return true
                        }else{
                            return false
                        }
                    }else{
                        return false
                    }
                }else if(data.level == 3) {
                    console.log(data, 'data3')
                    if((data.goodsQty < 1) ) {
                        return false;
                    }else{
                        return true;
                    }
                }
                
            },
            handleClick() {
                if(this.editType == 1) {
                    this.$store.dispatch('changeEditOn', true)
                    this.$store.dispatch('changeEditOut', false)
                    this.$store.dispatch('changeEditIn', false)
                }else if(this.editType == 2) {false
                    this.$store.dispatch('changeEditOn', false)
                    this.$store.dispatch('changeEditOut', true)
                    this.$store.dispatch('changeEditIn', false)
                }else if(this.editType == 3) {
                    this.$store.dispatch('changeEditOn', false)
                    this.$store.dispatch('changeEditOut', false)
                    this.$store.dispatch('changeEditIn', true)
                }
            },
            handleCheckChange(val, data) {
                var arr= this.$refs.retreatTpl.getCheckedNodes(true, false)
                this.selectData= arr
            },
            /**左侧按钮 */
            handleClickFirst() {
                if(this.selectData.length < 1) {
                    this.$alert('选择商品')
                }else {
                    if(this.editType == 1) {
                        var oldVals= JSON.parse(JSON.stringify(this.$store.state.retreatTpl.stockOnData))
                        let outArr= this.$store.state.retreatTpl.stockOutData
                        this.selectData.forEach(item => {
                            this.selectDataFun(outArr, item)
                        })
                        this.$store.dispatch('changeStockOutData', JSON.parse(JSON.stringify(outArr)))
                        this.changeNumFun(oldVals, this.selectData)
                        this.$store.dispatch('changeStockOnData', oldVals)
                    }else if(this.editType == 2) {
                        var oldVals= JSON.parse(JSON.stringify(this.$store.state.retreatTpl.stockOutData))
                        let onArr= this.$store.state.retreatTpl.stockOnData
                        this.selectData.forEach(item => {
                            this.selectDataFun(onArr, item)
                        })
                        this.$store.dispatch('changeStockOnData', JSON.parse(JSON.stringify(onArr)))
                        this.changeNumFun(oldVals, this.selectData)
                        this.$store.dispatch('changeStockOutData', oldVals)
                    }else if(this.editType == 3) {
                        var oldVals= JSON.parse(JSON.stringify(this.$store.state.retreatTpl.stockInData))
                        let onArr= this.$store.state.retreatTpl.stockOnData
                        this.selectData.forEach(item => {
                            this.selectDataFun(onArr, item)
                        })
                        this.$store.dispatch('changeStockOnData', JSON.parse(JSON.stringify(onArr)))
                        this.changeNumFun(oldVals, this.selectData)
                        this.$store.dispatch('changeStockInData', oldVals)
                    }
                }
                this.$nextTick(() => {
                    this.$refs.retreatTpl.filter()
                })
               
            },

             /**右侧按钮 */
            handleClickSecond() {
                if(this.selectData.length < 1) {
                    this.$alert('选择商品')
                }else {
                    if(this.editType == 1) {
                        var oldVals= JSON.parse(JSON.stringify(this.$store.state.retreatTpl.stockOnData))
                        let inArr= this.$store.state.retreatTpl.stockInData
                        console.log(inArr, 'inarr')
                        this.selectData.forEach(item => {
                            this.selectDataFun(inArr, item)
                        })
                        console.log(inArr, 'sele')
                        this.$store.dispatch('changeStockInData', JSON.parse(JSON.stringify(inArr)))
                        
                        this.changeNumFun(oldVals, this.selectData)
                        this.$store.dispatch('changeStockOnData', oldVals)
                    }else if(this.editType == 2) {
                        var oldVals= JSON.parse(JSON.stringify(this.$store.state.retreatTpl.stockOutData))
                        let inArr= this.$store.state.retreatTpl.stockInData
                        this.selectData.forEach(item => {
                            this.selectDataFun(inArr, item)
                        })
                        this.$store.dispatch('changeStockInData', JSON.parse(JSON.stringify(inArr)))
                        this.changeNumFun(oldVals, this.selectData)
                        this.$store.dispatch('changeStockOutData', oldVals)
                    }else if(this.editType == 3) {
                        var oldVals= JSON.parse(JSON.stringify(this.$store.state.retreatTpl.stockInData))
                        let outArr= this.$store.state.retreatTpl.stockOutData
                        this.selectData.forEach(item => {
                            this.selectDataFun(outArr, item)
                        })
                        this.$store.dispatch('changeStockOutData', JSON.parse(JSON.stringify(outArr)))
                        this.changeNumFun(oldVals, this.selectData)
                        this.$store.dispatch('changeStockInData', oldVals)
                    }
                }
                this.$nextTick(() => {
                    this.$refs.retreatTpl.filter()
                })
              
            },
            selectDataFun(arr, val) { 
                val.goodsQtyPc= val.goodsQty
                if(arr.length == 0) { 
                    arr.push({
                        brand: val.brandName,
                        level: 1,
                        children: [
                            {
                                code: val.goodsCode,
                                level: 2,
                                children: [val]
                            }
                        ]
                    })
                }else {
                    let brandArr= [],
                        addBrand= false;
                    for(let i= 0; i< arr.length; i++ ){
                        /**相同品牌 */
                        if(arr[i].brand == val.brandName) {
                            addBrand= true
                            if(arr[i].children.length == 0) {
                                arr[i].children.push({
                                    code: val.goodsCode,
                                    level: 2,
                                    children: [val]
                                })
                            }else{
                                let codeArr= []
                                let isAdd= false
                                for(let k=0; k< arr[i].children.length; k++) {
                                    /**相同品类 */
                                    if(arr[i].children[k].code == val.goodsCode) {
                                        isAdd= true
                                        if(arr[i].children[k].children.length == 0) {
                                            arr[i].children[k].children.push(val)
                                        }else{
                                            let arr1= [],
                                                addSku= false;
                                            for(let n=0; n< arr[i].children[k].children.length; n++) {
                                                if(arr[i].children[k].children[n].skuCode == val.skuCode) {
                                                    addSku= true
                                                    console.log(val.skuCode)
                                                    arr[i].children[k].children[n].goodsQty= arr[i].children[k].children[n].goodsQty +val.goodsQty
                                                    arr[i].children[k].children[n].goodsQtyPc= arr[i].children[k].children[n].goodsQtyPc +val.goodsQtyPc
                                                    break;
                                                }else{
                                                   
                                                }
                                            }
                                            if(!addSku) {
                                                arr1.push(val)  
                                            }
                                            arr[i].children[k].children=arr[i].children[k].children.concat(arr1)
                                        }
                                        break;
                                    } 
                                }
                                if(!isAdd) {
                                    codeArr.push({
                                        code: val.goodsCode,
                                        level: 2,
                                        children: [val]
                                    })
                                }
                                arr[i].children= arr[i].children.concat(codeArr)
                            }
                            break;
                        }else{
                            // arr.push({
                            //     brand: val.brandName,
                            //     level: 1,
                            //     children: [
                            //         {
                            //             code: val.goodsCode,
                            //             level: 2,
                            //             children: [val]
                            //         }
                            //     ]
                            // })
                        }
                    }
                    if(!addBrand) {
                        brandArr.push({
                            brand: val.brandName,
                            level: 1,
                            children: [
                                {
                                    code: val.goodsCode,
                                    level: 2,
                                    children: [val]
                                }
                            ]
                        })
                    }
                    if(brandArr.length > 0) {
                        brandArr.forEach(item => {
                            arr.push(item)
                        })
                    }
                    // arr= arr.concat(JSON.parse(JSON.stringify(brandArr)))  
                }
                console.log(arr, 'inarr')
                return arr;
            },

            /**减数量 */
            changeNumFun(arr, arr1) {
                /**arr原数据源，    arr1选中的子节点 */
                arr1.forEach(item => {
                    for(let i=0; i <arr.length; i++) {
                        for(let m=0; m < arr[i].children.length; m++) {
                            for(let n=0; n<arr[i].children[m].children.length; n++) {
                                if(item.skuCode == arr[i].children[m].children[n].skuCode) {
                                    arr[i].children[m].children[n].goodsQty= arr[i].children[m].children[n].goodsQtyPc-item.goodsQty
                                    arr[i].children[m].children[n].goodsQtyPc=  arr[i].children[m].children[n].goodsQty 
                                }
                            }
                        }
                    }
                })
                // // 晒出数量为0的
                // if(arr.length > 0) {
                //     for(let a=0; a< arr.length; a++) { // 获取品牌
                //         for(let b= 0; b< arr[a].children.length; b++) { // 获取code
                //             for(let c=0; c < arr[a].children[b].children.length; c++) { // 获取商品
                            
                //                 if(arr[a].children[b].children[c].goodsQty <1) {
                //                     arr[a].children[b].children.splice(c,1)
                //                     continue
                //                 }
                //             }
                //         }
                //     }
                // }
                // console.log(arr)
                return arr;
            },
            getNum(arr) {
                let sum1= 0, sum2=0, sum3= 0;
                if(arr.length > 0) {
                    for(let a=0; a< arr.length; a++) { // 获取品牌
                        for(let b= 0; b< arr[a].children.length; b++) { // 获取code
                        sum1+= sum2
                         arr[a].children.sum= sum1
                            for(let c=0; c < arr[a].children[b].children.length; c++) { // 获取商品
                                sum2+= arr[a].children[b].children[c].goodsQty
                                arr[a].children[b].sum= sum2
                                if(arr[a].children[b].children[c].goodsQty <1) {
                                    arr[a].children[b].children.splice(c,1)
                                    continue
                                }
                            }
                        }
                    }
                }
            }
        }
    }
</script>
<style lang="less" scoped>
    .text {
        font-size: 12px;
    }

    .item {
        margin-bottom: 10px;
    }

    .clearfix:before,.clearfix:after {
        display: table;
        content: "";
    }
    .clearfix:after {
        clear: both
    }
</style>